<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2018/4/27
 * Time: 11:47
 */
namespace app\common\model;

use think\Model;

class Check{
    private $tel;
    private $authCode;
    private $passd;

    private $mysql;

    function __construct(){
        $this->mysql = new Mysql();
    }

    function isBankNumberValid($no){
        $arr_no = str_split($no);
        $last_n = $arr_no[count($arr_no)-1];
        krsort($arr_no);
        $i = 1;
        $total = 0;
        foreach ($arr_no as $n){
            if($i%2==0){
                $ix = $n*2;
                if($ix>=10){
                    $nx = 1 + ($ix % 10);
                    $total += $nx;
                }else{
                    $total += $ix;
                }
            }else{
                $total += $n;
            }
            $i++;
        }
        $total -= $last_n;
        $x = 10 - ($total % 10);
        if($x == $last_n){
            return true;
        }

        return false;
    }

    function isRegisterNull($tel, $authCode, $passd) {
        if (empty($authCode) || mb_strlen($authCode, "UTF-8") != 6) {
            echo echoJson(0, "验证码为空或不合法!");
            return false;
        }

        return $this->isLoginNull($tel, $passd);
    }

    function isPhoneExists($tel){
        $sql = "SELECT * FROM user where tel='$tel'";

        if ($this->mysql->selectSQLRow($sql)) {//判断账号是否已存在
            echo echoJson(0, "电话已被注册，请使用其他电话注册!");
            return true;
        } else {
            return false;
        }
        return false;
    }

    function isfindTelExists($tel) {
        $sql = "SELECT tel FROM user where tel='$tel'";
        if ($this->mysql->selectSQLRow($sql)) {//判断账号是否已存在
            return true;
        } else {
            echo echoJson(0, "电话不存在，请重新输入!");
            return false;
        }
        return true;
    }
    /**
     * 电话号是否存在
     * @param str $tel 电话
     * @param str $passd 密码
     */
    function isLoginNull($tel,  $passd)
    {
        if (empty($tel) || mb_strlen($tel, "UTF-8") != 11) {
            echo echoJson(0, "电话号为空或不合法!");
            return false;
        }
        if (empty($passd) || mb_strlen($passd, "UTF-8") < 8 || mb_strlen($passd, "UTF-8") > 16) {
            echo echoJson(0, "密码为空或不合法!!");
            return false;
        }
        return true;
    }

    /**
     * 电话号是否存在
     * @param str $tel 电话
     * @param str $passd 密码
     */
    function isTelExists($tel, $authCode, $passd,$action) {
        $sql = "SELECT * FROM user where tel='$tel'";
        if ($action === "REGISTER_ACCOUNT") {
            if ($this->mysql->selectSQLRow($sql)) {//判断账号是否已存在
                echo echoJson(0, "电话已被注册，请使用其他电话注册!");
                return false;
            } else {
                return $this->SMS_verification($tel, $authCode, $passd, $action);
            }
        }else if($action === "FIND_PASSWORD"){
            $sql = "SELECT tel FROM user where tel='$tel'";
            if ($this->mysql->selectSQLRow($sql)) {//判断账号是否已存在
                return $this->SMS_verification($tel, $authCode, $passd, $action);
            } else {
                echo echoJson(0, "电话不存在，请重新输入!");
            }
        }
        return true;
    }

    function SMS_verification($tel, $authCode, $passd,$action) {
        $sql = "SELECT * FROM sms_verification where tel='$tel'";
        $row = $this->mysql->selectSQLRow($sql);
        if ($action === "REGISTER_ACCOUNT") {//注册
            if ($row['register_code'] == $authCode && time() <= ($row['register_time'] + 1000 * 10)) {//判断验证码是否匹配
                //getSelectUid($tel, $passd);
                return true;
            } else {
                echo echoJson(0, "验证码错误或已过期!");
                return false;
            }
        }else if ($action === "FIND_PASSWORD") {
            if ($row['find_password_code'] == $authCode && time() <= ($row['find_password_time'] + 1000 * 10)) {//判断验证码是否匹配
                //fandPssd($tel, $passd);
                return true;
            } else {
                echo time();
                echo echoJson(0, "验证码错误或已过期!");
                return false;
            }
        }
        return true;
    }

    /**
     *  判断账号和商品ID是否为空
     * @param str $uid 账号
     * @return boolean
     */
    function isGoodsNull($action, $case_id) {
        if (empty($action) || $action == "undefined") {
            echo echoJson(0, "参数错误，-001!");
            return false;
        }
        if (empty($case_id) || $case_id == "undefined") {
            echo echoJson(0, "参数错误，-002!");
            return false;
        }
        return true;
    }

    function isUidExist($uid){
        $sql = "SELECT * FROM user where uid='".$uid."'";
        $row = $this->mysql->selectSQLRow($sql);

        return $row;
    }
    /**
     *  判断账号和商品ID是否为空
     * @param str $uid 账号
     * @return boolean
     */
    function isPayNull($uid, $subject,$total_amount) {
        if (empty($uid) || !$this->isUidExist($uid)) {
            echo echoJson(0, "参数错误，-001!");
            return false;
        }
        if (empty($subject) || $subject == "undefined") {
            echo echoJson(0, "参数错误，-002!");
            return false;
        }
        if (empty($total_amount) || $total_amount == "undefined") {
            echo echoJson(0, "参数错误，-003!");
            return false;
        }

        return true;
    }
}